#  Copyright (c) 2010
#  The Regents of the University of Michigan
#  All Rights Reserved

#  Permission is granted to use, copy, create derivative works, and
#  redistribute this software and such derivative works for any purpose,
#  so long as the name of the University of Michigan is not used in
#  any advertising or publicity pertaining to the use or distribution
#  of this software without specific, written prior authorization. If
#  the above copyright notice or any other identification of the
#  University of Michigan is included in any copy of any portion of
#  this software, then the disclaimer below must also be included.

#  This software is provided as is, without representation or warranty
#  of any kind either express or implied, including without limitation
#  the implied warranties of merchantability, fitness for a particular
#  purpose, or noninfringement.  The Regents of the University of
#  Michigan shall not be liable for any damages, including special,
#  indirect, incidental, or consequential damages, with respect to any
#  claim arising out of or in connection with the use of the software,
#  even if it has been or is hereafter advised of the possibility of
#  such damages.
from .rules_helpers import refine, rule

__all__ = []


@rule
def video_high_confidence(name_tokens, tokens, biggest_extension):
  if any(x in tokens
         for x in ('dvdrip', 'xvid', 'h264', 'season', 'brrip', 'dvdr', 'x264',
                   'bluray')):
    return 'video'
  if biggest_extension in ('avi', 'mpg', 'mpeg', 'm4v', 'mkv', 'wmv', 'm2ts',
                           'rmvb', 'flv', 'vob', 'otrkey', 'divx', 'mov'):
    return 'video'


@rule
def audio_high_confidence(name_tokens, tokens, biggest_extension):
  if 'remix' in tokens:
    return 'audio'
  if biggest_extension in ('mp3', 'm4a', 'wma', 'flac', 'ogg', 'mka', 'shn',
                           'ape'):
    return 'audio'


@rule
def games_high_confidence(name_tokens, tokens, biggest_extension):
  if any(x in tokens
         for x in ('ps2', 'psp', 'nds', 'gba', 'xbox', 'psx', 'nocd',
                   'ps2dvd', 'wii', 'gamecube', 'nintendo', 'sega',
                   'xbox360', 'x360')):
    return 'games'
  if biggest_extension in ('bik', 'mpak'):
    return 'games'


@rule
def software_high_confidence(name_tokens, tokens, biggest_extension):
  if biggest_extension in ('exe', 'dmg', 'dll', 'msi', 'apk', 'ipa', 'cab'):
    return 'software'

  if 'windows' in tokens and any(x in tokens for x in ('xp', 'vista', '7')):
    return 'software'


# Audiobooks are officially audio.
@rule
def book_high_confidence(name_tokens, tokens, biggest_extension):
  if any((x in tokens or x + 's' in tokens) for x in ('handbook', 'ebook', 'books')):
    return 'book'
  if biggest_extension in ('pdf', 'epub', 'rtf', 'lit'):
    return 'book'

@rule
def images_high_confidence(name_tokens, tokens, biggest_extension):
  if biggest_extension in ('jpg', 'png', 'gif'):
    return 'images'


@rule
def video_low_confidence(name_tokens, tokens, biggest_extension):
  if biggest_extension == 'mp4':
    return 'video'


@rule
def audio_low_confidence(name_tokens, tokens, biggest_extension):
  if name_tokens[0] == 'va':
    return 'audio'
  if 'discography' in tokens or 'instrumental' in tokens or 'mix' in tokens:
    return 'audio'
  
  if 'instrumentals' in name_tokens:
    return 'audio'

  if biggest_extension == 'wav':
    return 'audio'

@rule
@refine('video', 'images')
def porn_low_confidence(name_tokens, tokens, biggest_extension):
  if any(x in tokens for x in ('fuck', 'fucked', 'fucking', 'slut', 'sluts',
                               'whore', 'whores', 'orgy', 'incest', 'rape',
                               'raped', 'cum', 'anal', 'teen', 'teens', 
                               'milf', 'pussy', 'cunt', 'cunts',
                               'xxx', 'tits', 'titties', 'nude', 'nudes',
                               'girls', 'dildo', 'amateur', 'playboy',
                               'bareback', 'facial', 'bestiality')):
    return 'porn'

  if 'ass' in name_tokens:  # .ass is a subtitle extension.
    return 'porn'
  if any('porn' in x for x in tokens):
    return 'porn'


@rule
def software_low_confidence(name_tokens, tokens, biggest_extension):
  if any(x in name_tokens for x in ('pro', 'full', 'antivirus', 'security')):
    return 'software'
  if any(x in tokens for x in ('serial', 'serials', 'keygen', 'microsoft', 'plugin',
                               'plugins', 'sp1', 'sp2', 'sp3', 'powerpoint')):
    return 'software'
  if 'excel' in tokens and 'saga' not in tokens:
    return 'software'


@rule
def games_low_confidence(name_tokens, tokens, biggest_extension):
  if 'pcdvd' in name_tokens or 'pc' in name_tokens or 'pal' in tokens or 'ds' in tokens:
    return 'games'


@rule
def book_low_confidence(name_tokens, tokens, biggest_extension):
  if 'manual' in name_tokens:
    return 'book'
  # Comic books.
  if biggest_extension in ('cbr', 'cbz'):
    return 'book'

@rule
def font(name_tokens, tokens, biggest_extension):
  if biggest_extension in ('ttf', 'otf'):
    return 'font'

@rule
def generic_disc(name_tokens, tokens, biggest_extension):
  if biggest_extension in ('iso', 'bin', 'mdf', 'daa', 'isz', 'nrg'):
    return 'disc'

@rule
def generic_archive(name_tokens, tokens, biggest_extension):
  if biggest_extension in ('zip', '7z', 'rar', 'tar', 'gz', 'bz2', 'tgz', 'tbz'):
    return 'archive'
  if biggest_extension and biggest_extension[0] == 'r' and biggest_extension[1:].isdigit():
    return 'archive'


@refine('archive', 'disc')
def specific_games(name_tokens, tokens, biggest_extension):
  if any(x in tokens for x in ('starcraft', 'bioshock', 'fallout')):
    return 'games'

@refine('archive', 'disc')
def specific_software(name_tokens, tokens, biggest_extension):
  if any(x in tokens for x in ('autocad','matlab', 'mathematica')):
    return 'software'

@refine('disc')
def linux_distros(name_tokens, tokens, biggest_extension):
  if any(x in tokens for x in ('debian', 'ubuntu', 'fedora', 'mandrake')):
    return 'software'
